/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jili.ubert.clientapi;

import com.jili.ubert.code.server2client.MsgResult;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 *
 * @author ChengJili
 */
class HistoryDataNettyClient {

    private Channel sendchannel;
    private String host;
    private int port;
    MsgResult Connect(String host, int port, boolean isSSL, boolean isZlib, HistoryDataRequest client) {
        MsgResult ret = new MsgResult();
        ret.success= false;
        // Configure the client.
        EventLoopGroup group = new NioEventLoopGroup();
        try {

            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .option(ChannelOption.TCP_NODELAY, true)
             .handler(new HistoryDataNettyClientInitializer(isSSL,isZlib,client));
            ChannelFuture f = b.connect(host, port).sync();
            this.sendchannel = f.channel();
            if (f.isDone()) {
                ret.setWord("连接成功");
                ret.success = true;
            }

        } catch (InterruptedException e) {
            ret.success = false;
            ret.setWord(e.getMessage());   
        } finally {
        }
        return ret;
    }
    public void SendMsg(int cmd,int RequstID,byte[] data){
        MsgEntity msg = new MsgEntity();
        msg.SendMsg(sendchannel, cmd, RequstID, data);
    }
    public HistoryDataClientHanlder SendSYNCMsg(int cmd,int RequstID,byte[] data){
        MsgEntity msg = new MsgEntity();
    //    TradeClientHanlder handler = ch.pipeline().get(TradeClientHanlder.class);
        msg.SendMsg(sendchannel, cmd, RequstID, data);
        return sendchannel.pipeline().get(HistoryDataClientHanlder.class);
        
    }
    public Channel getChannel(){
        return sendchannel;
    }

    MsgResult getDAOoperationAnswer() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    
}
